/**
 * related to listType.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-09-26
 */
var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");

var helperOpen = require("helper/open");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");

var listdata = mm("do_ListData");
var listView_types = ui("listView_types");
listView_types.bindItems(listdata);

//andriod返回键
page.on("back", function() {
    app.closePage();
});
/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);

var firstTypeId;
page.on("loaded", function() {
	var data = page.getData();
	firstTypeId = data.firstTypeId;
	if (!firstTypeId || firstTypeId != "") {
		refreshAllData();
	}
})

function refreshAllData(){
	qqHttp.post(homemakingUtil.url.type.findChildren, {parentId:firstTypeId}, function(data) {
		listView_types.rebound();
		hashdata.addData({
		    "labelTitle" : data.parentName
		})
		header.refreshData();
		listdata.removeAll();
		listdata.addData(data.childrenList);
		if (firstTypeId == homemakingUtil.typeId.JiaDianWeiXiu) {
			var dictList1 = [], dictList2 = [];
			if (data.dictList) {
				var dictList = data.dictList;
				for (var k = dictList.length-1; k >= 0; k--) {
					if (dictList[k].hasImg == "0") {
						dictList1.push({"template": 1,"dictTitle":dictList[k].dictTitle,"dictContent":dictList[k].dictContent});
					} else {
						dictList2.push({"template": 2,"dictTitle":dictList[k].dictTitle,"dictContents":dictList[k].dictContents});
					}
				}
			}
			listdata.addData(dictList1);
			listdata.addData(dictList2);
		}
		listView_types.refreshItems();
	});
}

//下拉列表，刷新数据
listView_types.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});

//点击一条
listView_types.on("touch", function(data){
	var one = listdata.getOne(data);
	if (one.parentId == homemakingUtil.typeId.JiaDianWeiXiu) { //家电维修
		helperOpen.open("source://view/homemaking/detail/toOrder3.ui",{id:one.id});
	} else {
		helperOpen.open("source://view/homemaking/detail/toOrder.ui",{typeId:one.id});
	}
});